<template>
  <n-button
    :type="computedType"
    :loading="loading"
    :disabled="disabled || loading"
    v-bind="$attrs"
    @click="handleClick"
  >
    <template #icon>
      <slot name="icon" />
    </template>
    <slot />
  </n-button>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  type: {
    type: String,
    default: 'default'
  },
  danger: Boolean,
  loading: Boolean,
  disabled: Boolean,
  throttle: {
    type: [String, Number],
    default: 0
  }
})

const emit = defineEmits(['click'])

let lastClickTime = 0
const handleClick = (e) => {
  const now = Date.now()
  // 确保throttle被转换为数字进行比较
  if (now - lastClickTime < Number(props.throttle)) return
  lastClickTime = now
  emit('click', e)
}

const computedType = computed(() => {
  return props.danger ? 'error' : props.type
})
</script>